/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.lib.tools;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

/**
 *
 * @author empcl_000
 */
public class xml {

    private NodeList n;

    private String fileName;

    public xml(String fileName) {
        this.fileName = fileName;
        this.getXmlNodes();
    }

    public org.w3c.dom.Document getDocument() {

        try {

            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(this.fileName);

            return doc;
        } catch (Exception e) {
            System.out.println("Error reading configuration file:");
            System.out.println(e.getMessage());
        }
        return null;
    }

    public void getXmlNodes() {

        Node root = getDocument().getChildNodes().item(1);
        this.n = root.getChildNodes();

    }

    public String[] getXmlNodesNames() {

        String[] rData = new String[this.n.getLength()];
        for (int i = 0; i < this.n.getLength(); i++) {
            if (this.n.item(i).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
                rData[i] = this.n.item(i).getNodeName();
            }
        }
        return rData;
    }

    public String[] getXmlNodesValue() {

        String[] rData = new String[n.getLength()];
        for (int i = 0; i < n.getLength(); i++) {
            if (n.item(i).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
                rData[i] = n.item(i).getTextContent();
            }
        }
        return rData;
    }
    ac.uitls.propertiesOperation op = new ac.uitls.propertiesOperation();

    public Map<String, String> getXmlAttars(String type) {

        NodeList n = getDocument().getElementsByTagName(type);

        NamedNodeMap attrs = n.item(0).getAttributes();

        Map<String, String> m = new HashMap<String, String>();

        for (int i = 0; i < attrs.getLength(); i++) {
            m.put(attrs.item(i).getNodeName(), op.getWord(attrs.item(i).getNodeValue()));
        }

        return m;
    }
}
